#pragma once
#include "afxcmn.h"
#include "afxwin.h"


#include "Poco\SharedPtr.h"

// CManageAnswerDatabase dialog

class CManageAnswerDlg : public CDialog
{
    DECLARE_DYNAMIC(CManageAnswerDlg)

public:
    CManageAnswerDlg(CWnd* pParent = NULL);   // standard constructor
    virtual ~CManageAnswerDlg();

// Dialog Data
    enum { IDD = IDD_ManageAnswerDatabase };

public:
    void Initialize(void);

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    DECLARE_MESSAGE_MAP()

    CListCtrl m_listStudents;
    CListBox m_listQuestionTypes;
    CListCtrl m_listQuestions;
    CRichEditCtrl m_editTitle;
    CEdit m_editScore;
    CButton m_btnEditScore;

    int m_lastQuestionTypeSel;

    bool m_flagSortingQuestion;

    int m_selectedStudentIdx;
    ExamSystem::QuestionType m_selectedQuestionType;
    int m_selectedQuestionIdx;

    int m_selectedStudentItem;
    int m_selectedQuestionItem;

    Poco::SharedPtr<ExamSystem::CAnswer> m_currentAnswer;
    Poco::SharedPtr<ExamSystem::CCriterion> m_currentCriterion;
    Poco::SharedPtr<ExamSystem::CQuestion> m_currentQuestion;

    std::string m_answerTempPath;
    std::string m_answerSubPath;
    std::string m_criterionTempPath;
    std::string m_criterionnSubPath;

    typedef struct QuestionId
    {
        std::vector<int> ids[ExamSystem::QuestionTypeCount];
        std::vector<int> &operator [](int idx) { return ids[idx]; }
    } QuestionId;

    typedef struct QuestionScores
    {
        std::vector<double> scores[ExamSystem::QuestionTypeCount];
        std::vector<double> &operator [](int idx) { return scores[idx]; }
    };

    QuestionId m_questionIds;
    std::vector<ExamSystem::StudentInfo> m_students;
    std::vector<double> m_scores;
    std::vector<QuestionId> m_studentQuestionIds;
    std::vector<QuestionScores> m_studentQuestionScores;

    CStatic m_wndReferenceAnswer;
    CStatic m_wndStudentAnswer;
    CRichEditCtrl m_editReferenceAnswer;
    CListCtrl m_listReferenceAnswer;
    CRichEditCtrl m_editStudentAnswer;
    CListCtrl m_listStudentAnswer;

    void ClearAndSetUp(void);
    void QuestionSelected(void);
    void UpdateScoreEditBtnUI(void);
    int GetStudentQuestionIdx(void);
    void ShowQuestionTitle(void);
    void ResetAnswerWindows(void);
    virtual void OnOK();
    virtual void OnCancel();

protected:
    typedef void (CManageAnswerDlg::*ShowFunc)(void);
    // show question
    void ShowChoiceQuestion(void);
    void ShowFillingQuestion(void);
    void ShowTypingQuestion(void);
    void ShowQNAQuestion(void);
    void ShowOfficeQuestion(void);
    void ShowCodingQuestion(void);
    void ShowHtmlQuestion(void);
    void ShowPhotoshopQuestion(void);
    void ShowFiledQuestion(void);
    void ShowPropertyQuestion(void);
    static ShowFunc ms_showQuestionFuncs[ExamSystem::QuestionTypeCount];

    // show question
    void ShowChoiceAnswer(void);
    void ShowFillingAnswer(void);
    void ShowTypingAnswer(void);
    void ShowQNAAnswer(void);
    void ShowOfficeAnswer(void);
    void ShowCodingAnswer(void);
    void ShowHtmlAnswer(void);
    void ShowPhotoshopAnswer(void);
    void ShowFiledAnswer(void);
    void ShowPropertyAnswer(void);
    static ShowFunc ms_showAnswerFuncs[ExamSystem::QuestionTypeCount];

public:
    afx_msg void OnBnClickedCancel();
    afx_msg void OnBnClickedOpenQuestionLibrary();
    virtual BOOL OnInitDialog();
    afx_msg void OnNMClickListStudents(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void OnLbnSelchangeListQuestionType();
    afx_msg void OnLbnSelchangeListQuestions();
    afx_msg void OnNMClickListQuestions(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void OnLvnColumnclickListStudents(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void OnLvnColumnclickListQuestions(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void OnBnClickedButtonModifyScore();
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    afx_msg void OnNMDblclkListStudentAnswer(NMHDR *pNMHDR, LRESULT *pResult);
};
